import Compatible from "./compatible";
export default {
  mixins: [Compatible],
  data() {
    return {
      // token: ""
    }
  },
  mounted(){
    // this.token = this.getCookie('intoToken');
  },
  methods: {
    /**
     * POST请求
     * @param {Object} params post传递的参数以及URL方法，建议token在这里传递，等扩展
     * @param {Boolean} isProcessing 是否需要处理返回状态
     * @description <code>
     *  this.post({
     *    url: '/card-service-web/h5/chestRecords/sacrifice',
     *    headers: {accessToken: _this.getCookie('intoToken')},
     *    params: {...params}
     *  }).then(body => {
     *    return "123";
     *  }).then(info => {
     *    // info === "123" info是根据前一个then返回的值决定的 如果不懂可以去了解下promise
     *    return "5667";
     *  });
     *
     * </code>
     */
    post(url, options, isProcessing) {
      if(typeof options === "boolean"){
        isProcessing = options;
      }
//    console.log(this.token, "token");
      return this.$http({
        method: "POST",
        url: url,
        params: options || {},
        headers: {
          auth: 'eyJhbGciOiJIUzUxMiJ9.eyJpaGF2ZXUiOiJqcm1ldXAiLCJzdWIiOiIxIiwiZXhwIjoxNTI2MjkwMTM0LCJpYXQiOjE1MjU2ODUzMzR9.55kG62viksBg0V9hmOj8HOLkj8IzUDmH0XmCby9Axr2TEtgNjaYdjlP3z4onYfjri2qyn0-bKaCCwnpTS4hH5w'
        }
      }).then(res => {
        if(isProcessing){
          return res;
        }
        // 在此处做一些错误处理

        // 获取请求状态码
        const status = +res.body.code;
        // 如果登入失效则跳转登录
        if(status === "4003") {
          // this.goLogin();
          // throw new Error("登录失效，请重新登录");
        }
        if(+status !== 200 && +status !== 1708 && +status !== 5001 && +status !== 5002) {
          this.toast(res.body.message);
        }
        return res.body;
      });
    },
    /**
     * GET请求
     * @param {String} url 请求地址
     * @param {Object} options 请求参数
     * @param {Boolean} isProcessing 是否需要处理返回状态
     */
    get(url, options, isProcessing) {
      if(typeof options === "boolean"){
        isProcessing = options;
      }
      return this.$http({
        method: "GET",
        url: url,
        params: options || {},
        headers: {
          // accessToken: this.token
          auth: 'eyJhbGciOiJIUzUxMiJ9.eyJpaGF2ZXUiOiJqcm1ldXAiLCJzdWIiOiIxIiwiZXhwIjoxNTI2MjkwMTM0LCJpYXQiOjE1MjU2ODUzMzR9.55kG62viksBg0V9hmOj8HOLkj8IzUDmH0XmCby9Axr2TEtgNjaYdjlP3z4onYfjri2qyn0-bKaCCwnpTS4hH5w'
        }
      }).then(res => {
        if(isProcessing){
          return res;
        }
        // 获取请求状态码
        const status = +res.body.code;

        // 如果登入失效则跳转登录
        if(+status === 4003) {
          /*this.goLogin();
          throw new Error("登录失效，请重新登录");*/
        }
        if(+status === 1707){
          // 弹出绑定微信提示框
          // 如果当前对象没有更新绑定微信提示框方法则向父级调用，暂时支持2级
          if("updatePopState" in this.$listeners) {
            this.$emit("updatePopState", true);
          }else{
            this.$parent.$emit("updatePopState", true);
          }
        }
        if(+status !== 200 && +status !== 1708) {
          this.toast(res.body.message);
        }
        return res.body;
      });
    },
    // 可以分离部分
    /**
     * 按照机型设置滚动条属性
     */
    scroll: function () {
      let _this = this
      _this.clientType(function () {
        let html = document.documentElement,
          body = document.body,
          scrollTop = body.scrollTop;
        html.style.overflow = 'hidden';
        body.style.overflow = 'hidden';
        body.className = 'body-fixed';
        body.style.top = scrollTop * -1 + 'px';
      }, function () {
        document.body.addEventListener('touchmove', function (e) {
          if (e.target.className.indexOf('pop-upsMask') > -1 || e.target.className.indexOf('pop-upstitle') > -1 || e.target.className.indexOf('pop-upsClose') > -1) {
            e.preventDefault();
          }
        })
      });
    },



  }
}
